<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
    $this->load->model('User_model','users');
      $this->load->model('Operation_model', 'oper');
	}

	public function index()
	{
		
	}

  //添加客户页面
  public function addUser(){
    $id = $this->input->get('id');
    $data['id'] = '';
    if($id){
      $data['id'] = $id;
    }
    $this->load->view('addUser',$data);
  }

  //客户列表页面
  public function user(){
    $this->load->view('user');
  }

  //修改常用客户信息
  public function editUser(){
    $data = $this->input->post();
    if($this->users->editUserHandle(array(
      'id'=>$data['id']
    ),$data)){
      return TRUE;
    }else{
      return FALSE;
    }
  }

  //得到常用客户信息
  public function getUserList(){
    $data = $this->users->getRegulars();
    if($data){
      jechos($data);
    }else{
      jechof("获取常用客户信息失败");
    }
  }

  //添加商家页面
  public function addBusiness(){
    $id = $this->input->get('id');
    $data['id'] = '';
    if($id){
      $data['id'] = $id;
    }
    $this->load->view('addBusiness',$data);
  }

  //商家列表页面
  public function business(){
    $this->load->view('business');
  }

  //得到商家列表信息
  public function getBusinessList(){
    $data = $this->users->getBusiness();
    if($data){
      jechos($data);
    }else{
      jechof("获取常用客户信息失败");
    }
  }

	//初次设置安全密码
  public function setSeccodeFirst(){
      $seccode = $this->input->post('seccode');
      //检查是否为初次设置
      if(!$this->isSetSeccode_R()){
          if(strlen($seccode)<6){
              jechof('安全密码长度必须大于6位!');
          }else{
              $this->users->changeUserInfo(array(
                  'id'=>$_SESSION['id']
              ),array(
                  'seccode' => substr(md5($seccode), 8, 16)
              ));
              //更新SESSION
             $_SESSION = $this->users->getUserInfo(array('a.id'=>$_SESSION['id']));
             $this->oper->add(array(
              'operation'=>'设置安全密码成功',
              'ip'=> $this->input->ip_address(),
              'uid'=>$_SESSION['id'],
              'time'=> date('Y-m-d h:i:s',time())
            ));
             jechos('设置成功');
          }
      }else{
          jechof('用户初次安全密码已经设定,请使用修改功能!');
      }
  }
  //检查用户是否设置安全密码
  private function isSetSeccode_R(){
      if(empty($_SESSION['seccode'])){
          return FALSE;
      }else{
          return TRUE;
      }
  }


  //添加常用客户
  public function addPhraseHandle(){
    $data = $this->input->post();
    $data['time'] = date("Y-m-d H:i:s",time());
    $data['status'] = 0;
    $this->users->addPhrase();
  }

  //添加商家
  public function addBusinessHandle(){
    $data = $this->input->post();
    $data['time'] = date("Y-m-d H:i:s",time());
    $data['status'] = 0;
    $this->users->addBusiness();
  }
}

/* End of file User.php */
/* Location: ./application/controllers/User.php */
